package com.aididi.controller;

import com.aididi.entity.IUsers;
import com.aididi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @Auther: zx.xu
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {


    private IUserService userService;

    @Autowired
    public UserController(IUserService userService){
        this.userService = userService;
    }

    @ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "User not found")
    public void notFound(){}

    @GetMapping("list")
    public Flux<IUsers> list() {
        return userService.list();
    }

    @GetMapping("/{id}")
    public Mono<IUsers> getById(@PathVariable Integer id){
        return userService.getById(id);
    }

    @PostMapping("createOrUpdate")
    public Flux<IUsers> create(@RequestBody Flux<IUsers> users){
        return userService.createOrUpdate(users);
    }

    @PutMapping("/{id}")
    public Mono<IUsers> update(@PathVariable Integer id, @RequestBody IUsers user){
        Objects.requireNonNull(user);
        user.setId(id);
        return userService.createOrUpdate(user);
    }

    @DeleteMapping("/{id}")
    public Mono<IUsers> delete(@PathVariable Integer id){
        return userService.delete(id);
    }


}
